package xFGE.xDisplay.xUI
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	import xFGE.xDisplay.BitmapEx.BitmapSprite;
	
	/**
	 * 拖动条 
	 * @author Ben
	 * 
	 */
	public class xScrollBar extends Sprite
	{
		private var _dragBar:BitmapSprite;
		
		private var _upButton:xButton;
		
		private var _downButton:xButton;
		
		private var _scrollType:int = 0;
		
		private var _textField:TextField = null;
		
		private var _direct:int = 0;
		
		private var _timer:Timer;
		
		public function xScrollBar()
		{
			super();
			//TODO: implement function
			_timer = new Timer(150);
			_initView();
			
		}
		
		/**
		 * 设置被控制的text区域 
		 * @param text
		 * 
		 */
		public function SetTexFiled(text:TextField):void
		{
			_textField = text;
			Update();
		}
		/**
		 * 更新 
		 * 
		 */
		public function Update():void
		{
			if(_textField == null) return;
			_textField.scrollV = 1;
			if(_textField.maxScrollV  == 1){
				_upButton.SetEnabled(false);
				_downButton.SetEnabled(false);
			}else{
				_upButton.SetEnabled(true);
				_downButton.SetEnabled(true);
			}
		}
		/**
		 * 初始化试图 
		 * 
		 */
		private function _initView():void
		{
			_upButton = getChildByName("__pageUp") as xButton;
			_downButton = getChildByName("__pageDown") as xButton;
			
//			_upButton.addEventListener(MouseEvent.ROLL_OVER,_onUpOver);
//			_downButton.addEventListener(MouseEvent.ROLL_OVER,_onDownOver);
//			_upButton.addEventListener(MouseEvent.ROLL_OUT,_onBtnRollOut);
			_upButton.SetClickEnabled(false);
			_downButton.SetClickEnabled(false); 
			_upButton.addEventListener(MouseEvent.MOUSE_DOWN,_onUpMouseDown);
			_upButton.addEventListener(MouseEvent.MOUSE_UP,_onUpMouseUp);
			_downButton.addEventListener(MouseEvent.MOUSE_DOWN,_onDownMouseDown);
			_downButton.addEventListener(MouseEvent.MOUSE_UP,_onDownMouseUp);
			_downButton.addEventListener(MouseEvent.CLICK,_onDownMouseClick);
			_upButton.addEventListener(MouseEvent.CLICK,_onUpMouseClick);
			
			_timer.addEventListener(TimerEvent.TIMER,_onTimer);
			
		}
		
		private function _onDownMouseClick(eve:MouseEvent):void
		{
			if(_textField.scrollV == _textField.maxScrollV) return;
			_textField.scrollV +=1;
		}
		
		private function _onUpMouseClick(eve:MouseEvent):void
		{
			if(_textField.scrollV == 1) return;
			_textField.scrollV -=1;
		}
		/**
		 * 计时器 
		 * @param eve
		 * 
		 */
		private function _onTimer(eve:TimerEvent):void
		{
			//_textField.scrollV
			switch(_direct){
				case 0:
				{
					if(_textField.scrollV >= _textField.maxScrollV) _timer.stop();
					else _textField.scrollV+=3;
					break;
				}
				case 1:
				{
					if(_textField.scrollV <= 1) _timer.stop();
					else _textField.scrollV-=3;
					break;
				}
			}
		}
		
		private function _onUpMouseDown(eve:MouseEvent):void
		{
			_direct = 1;
			_timer.start();
			//addEventListener(Event.ENTER_FRAME,_onEnterFrame);
		}
		
		private function _onUpMouseUp(eve:MouseEvent):void
		{
		
			_timer.stop();
			_timer.reset()
			//addEventListener(Event.ENTER_FRAME,_onEnterFrame);
		}
		
		private function _onDownMouseDown(eve:MouseEvent):void
		{
			_direct = 0;
			_timer.start();
			//removeEventListener(Event.ENTER_FRAME,_onEnterFrame);
		}
		
		private function _onDownMouseUp(eve:MouseEvent):void
		{
			_timer.stop();
			_timer.reset()
		}
	}
}